from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ProductReviewPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.review_rating_locator = (By.ID, "review-rating")
        self.review_text_area_locator = (By.ID, "review-text-area")
        self.submit_review_button_locator = (By.ID, "submit-review-button")
        self.review_list_locator = (By.ID, "review-list")
        self.review_item_locator = (By.ID, "review-item")
        self.review_author_locator = (By.ID, "review-author")
        self.review_date_locator = (By.ID, "review-date")
        self.review_content_locator = (By.ID, "review-content")
        self.average_rating_locator = (By.ID, "average-rating")

    def navigate(self):
        self.driver.get('http://example.com/product/reviews')

    def set_rating(self, rating):
        rating_button = (By.ID, f"rating-{rating}")
        rating_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(rating_button)
        )
        rating_button.click()

    def enter_review_text(self, text):
        review_text_area = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.review_text_area_locator)
        )
        review_text_area.clear()
        review_text_area.send_keys(text)

    def submit_review(self):
        submit_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.submit_review_button_locator)
        )
        submit_button.click()

    def get_reviews(self):
        reviews = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.review_list_locator)
        )
        return reviews

    def get_review_author(self, review_id):
        review_author = (By.ID, f"author-{review_id}")
        review_author = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(review_author)
        )
        return review_author.text

    def get_review_date(self, review_id):
        review_date = (By.ID, f"date-{review_id}")
        review_date = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(review_date)
        )
        return review_date.text

    def get_review_content(self, review_id):
        review_content = (By.ID, f"content-{review_id}")
        review_content = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(review_content)
        )
        return review_content.text

    def get_average_rating(self):
        average_rating = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.average_rating_locator)
        )
        return average_rating.text